// mongoose文档模型
import { Schema, model } from 'mongoose'

interface ObjectWithStringKeys {
  [key: string]: Schema;
}
const Schemas: ObjectWithStringKeys = {}

// 定义一些文档名称常量
export const PROFILE = 'profile'
export const USER = 'user'
export const STUDENT = 'student'
export const RECORD = 'record'
export const MONEY = 'money'
export interface UserData {
  account: string;
  password: string;
  name: string;
  sex: string;
  grade: string;
  class: string;
  dept: string;
  position: string;
}
export interface StudentData {
  account: string;
  name: string;
  sex: string;
  grade: string;
  class: string;
  dept: string;
  position: string;
}
export interface ProFile {
  date: number;
  type: string;
  description: string;
  income: string;
  expend: string;
  remark: string;
  proof: Array<string>;
  dept: string;
  account: string;
  name: string;
  redate: number;
  state: Array<string>;
  balance: string;
  reaccount: string;
  rename: string;
  reproof: Array<string>;
  falseinfo: string;
}

Schemas[USER] = new Schema({
  account: { type: String, required: true },
  password: { type: String, required: true },
  identity: { type: String, required: true }
})
Schemas[STUDENT] = new Schema({
  account: { type: String, required: true },
  name: { type: String, required: true },
  sex: { type: String, required: true },
  grade: { type: String, required: true },
  class: { type: String, required: true },
  dept: { type: String, required: true },
  position: { type: String, required: true }
})
Schemas[PROFILE] = new Schema<ProFile>({
  date: { type: Number, default: Date.now },
  type: { type: String },
  description: { type: String },
  income: { type: String, required: true },
  expend: { type: String, required: true },
  remark: { type: String },
  proof: { type: Array, required: true },
  dept: { type: String, required: true },
  account: { type: String, required: true },
  name: { type: String, required: true },
  redate: { type: Number },
  state: { type: Array, required: true, default: 'audit' },
  balance: { type: String, required: true },
  reaccount: { type: String },
  reproof: { type: Array },
  rename: { type: String },
  falseinfo: { type: String }
})
Schemas[MONEY] = new Schema({
  id: { type: Number, required: true, default: 1 },
  balance: { type: String, required: true, default: '500' }
})

/**
 * 返回对应的Schema
 * @param name 模型名称
 */
export default (name: string) => model(name, Schemas[name])
